/*
 *
 * Copyright (c) 2021
 * User:Dybala
 * File:TaskController.java
 * Date:2021/11/18 16:58:18
 */

package com.controller.UserController;

import com.config.myAnnotation.Log;
import com.config.myAnnotation.PassToken;
import com.pojo.*;
import com.service.TaskService;
import com.utils.HttpContextUtils;
import com.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Api(tags="任务接口")
@RestController
@RequestMapping("/user/task")
public class TaskController {

    @Autowired
    private TaskService taskService;

    /**
     * 获取所有的待接任务
      * @return 一个任务list列表
     */
    @Log("获取所有待解任务")
    @CrossOrigin
    @ApiOperation("获取所有任务接口")
    @ApiResponses({
            @ApiResponse(code=401,message="权限不足"),
            @ApiResponse(code=200,message="获取成功")
    })
    @GetMapping("/tasks")
    public List<TaskMini> GetTasks(){
        List<TaskMini> tasks = taskService.getAllTasks();
        return tasks;
    }

    @Log("查看某项具体任务")
    @ApiResponses({
            @ApiResponse(code=401,message="权限不足"),
            @ApiResponse(code=200,message="获取成功")
    })
    @PassToken
    @CrossOrigin
    @ApiOperation("查看某项任务详情，同时希望在实现时也会给出发布者的一些历史记录")
    @GetMapping("/Detail")
    public Task GetTaskById(@RequestParam("id") int id){
//        System.out.println(1);
        Task task = taskService.getTaskById(id);
        return task;
    }

    @Log("添加任务")
    @ApiOperation("添加任务，前端参数包括title，content，tag_id,reward")
    @CrossOrigin
    @PostMapping("/addTask")
    public Message addTask(@RequestBody Task task){
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        String token = request.getHeader("token");
        String email = JwtUtils.getEmail(token);
        //加装一层信息，现在的task class属于不完整的
        task.setPublisher(email);
        // 返回message 的class
        Message message = taskService.addTask(task);

        return message;
    }

    @Log("查看自己发布的任务数量")
    @CrossOrigin
    @ApiOperation("count当前用户的所有发布的任务")
    @GetMapping("/count")
    public int countMyTask(){
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        String token = request.getHeader("token");
        String email = JwtUtils.getEmail(token);
        int count = taskService.getCount(email);
        return count;
    }

    @Log("获取自己发布的全部待接任务")
    @GetMapping("/mytasks")
    @CrossOrigin
    @ApiOperation("查看自己发布的全部待接任务")
    public List<Task> getAllMyTask(){
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        String token = request.getHeader("token");
        String email = JwtUtils.getEmail(token);
        List<Task> allMyTasks = taskService.getAllMyTasks(email);
        return allMyTasks;
    }

    @CrossOrigin
    @PostMapping("/withdraw")
    @ApiOperation("撤回自己发布的任务,参数为id:撤销任务的id")
    @Log("撤回自己发布的某项任务")
    public Message withdraw(@RequestBody Map<String, Object> map){
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        String token = request.getHeader("token");
        String email = JwtUtils.getEmail(token);
        //拿到id号
        int id=Integer.parseInt( (String) map.get("id"));
        return taskService.withDraw(email,id);
    }

    @CrossOrigin
    @ApiOperation("分类返回任务")
    @Log("分类查询任务")
    @GetMapping("/Classification")
    public List<Task>  classification(@RequestParam int tag_id){
        return taskService.getTaskByOrder(tag_id);
    }

    @CrossOrigin
    @ApiOperation("接受任务，参数包括接受任务的id")
    @Log("接受任务")
    @GetMapping("/accept")
    public Message acceptTask(@RequestParam int id){
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        String token = request.getHeader("token");
        String email = JwtUtils.getEmail(token);
        Message message = taskService.acceptTask(id, email);
        if(message.getFlag()==200)
        taskService.notifyTask(email,id);
        return message;
    }

    //查看自己接受的正在完成的任务
    @CrossOrigin
    @ApiOperation("查看自己正在执行的任务")
    @Log("查看正在执行的任务")
    @GetMapping("/tasks/working")
    public List<PubTask> inExecution(){
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        String token = request.getHeader("token");
        String email = JwtUtils.getEmail(token);
        List<PubTask> tasks = taskService.queryPub(email);
        return tasks;
    }
    //  点击查看正在进行的详细任务
    @CrossOrigin
    @ApiOperation("查看自己正在执行的某项详细任务信息")
    @Log("查看正在执行某项详细任务")
    @GetMapping("/tasks/inExecution")
    public PubTask checkInExecutionDetails(@RequestParam int id ){
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        String token = request.getHeader("token");
        String email = JwtUtils.getEmail(token);
        PubTask PubTask = taskService.queryPubDeatils(email, id);
        return PubTask;
    }


    //查看自己已完成的任务
    @CrossOrigin
    @ApiOperation("查看自己完成的任务列表")
    @Log("查看自己完成的任务列表")
    @GetMapping("/tasks/finishedTasks")
    public List<PubTask> queryFinished(){
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        String token = request.getHeader("token");
        String email = JwtUtils.getEmail(token);
        List<PubTask> pubTasks = taskService.queryFinished(email);
        return pubTasks;
    }

    //确认完成
    @CrossOrigin
    @ApiOperation("确认完成某项任务,参数为id：任务编号")
    @Log("确认完成某项任务")
    @PostMapping("/finish")
    public PubTask finishTask(@RequestBody Map<String, Object> map){
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        String token = request.getHeader("token");
        String email = JwtUtils.getEmail(token);
        //拿到id号
        int id=Integer.parseInt( (String) map.get("id"));
        PubTask task = taskService.finishTask(email, id);
        return task;
    }


    //查询已完成的详细信息,包含评论信息等。
    @CrossOrigin
    @ApiOperation("查看完成的任务的具体信息，参数为id：任务编号")
    @Log("查看已完成任务详情")
    @PostMapping("/check")
    public HistoryTask checkSpecial(@RequestBody Map<String, Object> map){
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        String token = request.getHeader("token");
        String email = JwtUtils.getEmail(token);
        //拿到id号
        int id=Integer.parseInt( (String) map.get("id"));
        return taskService.checkSpecial(email,id);
    }

    @CrossOrigin
    @ApiOperation("写评价")
    @Log("写评价")
    @PostMapping("/feedback")
    public Message feedback(@RequestBody Comment comment){
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        String token = request.getHeader("token");
        String email = JwtUtils.getEmail(token);
        Message message = taskService.writeComment(comment, email);
        return message;
    }

    @CrossOrigin
    @ApiOperation("投诉,传入任务task_id，投诉内容content")
    @Log("投诉事务")
    @PostMapping("/report")
    public Message complain(@RequestBody Report report){
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        String token = request.getHeader("token");
        String email = JwtUtils.getEmail(token);
        report.setLanucher(email);
        Message msg = taskService.reporter(report);
        return msg;
    }

    @CrossOrigin
    @ApiOperation("查看自己发布的完成的任务的列表")
    @Log("查看自己发布的已完成任务列表")
    @GetMapping("/hasfinishedtask")
    public List<Map<String ,Object>> getFinishedTaskList(){
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        String token = request.getHeader("token");
        String email = JwtUtils.getEmail(token);
        List<Map<String, Object>> finishedTask = taskService.getFinishedTask(email);
        return finishedTask;
    }
    @CrossOrigin
    @ApiOperation("举报任务不符合规范,传入任务task_id，投诉内容content")
    @Log("举报事务")
    @PostMapping("/reportPreTask")
    public Message reportPreTask(@RequestBody Report report){
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        String token = request.getHeader("token");
        String email = JwtUtils.getEmail(token);
        report.setLanucher(email);
        report.setType_id(1);
        Message message = taskService.reportPreTask(report);
        return message;
    }

    @CrossOrigin
    @ApiOperation("查看自己发布的正在被完成的任务")
    @Log("查看自己发布的正在被完成的任务")
    @GetMapping("/myworkingtask")
    public List<PubTask> getMyWokringTask(){
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        String token = request.getHeader("token");
        String email = JwtUtils.getEmail(token);
        List<PubTask> res = taskService.queryWorkers(email);
        return res;
    }


    @CrossOrigin
    @ApiOperation("在帖子详情页发表/回复评论，传入taskId, receiverEmail, content")
//    @Log("添加评论")
    @PostMapping("/addDiscussion")
    public Message addDiscussion(@RequestBody Discussion discussion) {
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        String token = request.getHeader("token");
        String senderEmail = JwtUtils.getEmail(token);

        discussion.setSenderEmail(senderEmail);
        Message msg = taskService.addDiscussion(discussion);
        return msg;
    }

    @CrossOrigin
    @ApiOperation("在帖子详情页查看与用户相关的评论，传入taskId。未登录或其他异常返回null。这个是有限制的查看")
    @Log("查看用户相关评论")
    @PostMapping("/myDiscussions")
    public List<Discussion> getMyDiscussions(@RequestBody Map<String, Object> map) {
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        String token = request.getHeader("token");
        String email = JwtUtils.getEmail(token);

        int taskId = Integer.parseInt((String) map.get("taskId"));
        List<Discussion> discussions = taskService.getMyDiscussions(taskId, email);
        return discussions;
    }

    @CrossOrigin
    @ApiOperation("在帖子详情页查看所有评论，传入taskId。未登录或其他异常返回null。这个是无限制的查看")
    @Log("查看所有评论")
    @PostMapping("/allDiscussions")
    public List<Discussion> getAllDiscussions(@RequestBody Map<String, Object> map) {
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        String token = request.getHeader("token");
        String email = JwtUtils.getEmail(token);

        int taskId = Integer.parseInt((String) map.get("taskId"));
        List<Discussion> discussions = taskService.getAllDiscussions(taskId, email);
        return discussions;
    }

}
